﻿using System.Windows;
using System.Windows.Controls;
using SnowmanConsulting.ProductKeyManager.Data;
using System;
using SnowmanConsulting.ProductKeyManager.Logging;
using System.Globalization;
using SnowmanConsulting.ProductKeyManager.UI;

namespace SnowmanConsulting.ProductKeyManager.Pages
{
    /// <summary>
    /// Interaction logic for ProductsPage.xaml
    /// </summary>
    public partial class ProductsPage : Page
    {
        public ProductCollection Products
        {
            get
            {
                return Properties.Settings.Default.Products;
            }
        }

        public SortDirection NameSortDirection
        {
            get
            {
                return Properties.Settings.Default.SortNameDirection;
            }
            set
            {
                Properties.Settings.Default.SortNameDirection = value;
                Properties.Settings.Default.Save();
            }
        }

        public SortDirection KeyCountSortDirection
        {
            get
            {
                return Properties.Settings.Default.SortKeyCountDirection;
            }
            set
            {
                Properties.Settings.Default.SortKeyCountDirection = value;
                Properties.Settings.Default.Save();
            }
        }

        public ProductsPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ProductsPage_Loaded);
            SetupEventHandlers();
        }

        private void SetupEventHandlers()
        {
            this.ProductsListBox.SelectionChanged += new SelectionChangedEventHandler(this.ProductsListBox_SelectionChanged);
            this.NameSortButton.Click += new RoutedEventHandler(this.NameSortButton_Click);
            this.KeyCountSortButton.Click += new RoutedEventHandler(this.KeyCountSortButton_Click);
        }

        void KeyCountSortButton_Click(object sender, RoutedEventArgs e)
        {
            switch (this.NameSortDirection)
            {
                case SortDirection.None:
                    this.KeyCountSortDirection = SortDirection.Ascending;
                    break;
                case SortDirection.Ascending:
                    this.KeyCountSortDirection = SortDirection.Descending;
                    break;
                case SortDirection.Descending:
                default:
                    this.KeyCountSortDirection = SortDirection.None;
                    break;
            }

            this.KeyCountSortButton.DataContext = this.KeyCountSortDirection;
        }

        void NameSortButton_Click(object sender, RoutedEventArgs e)
        {
            switch (this.NameSortDirection)
            {
                case SortDirection.None:
                    this.NameSortDirection = SortDirection.Ascending;
                    break;
                case SortDirection.Ascending:
                    this.NameSortDirection = SortDirection.Descending;
                    break;
                case SortDirection.Descending:
                default:
                    this.NameSortDirection = SortDirection.None;
                    break;
            }

            this.NameSortButton.DataContext = this.NameSortDirection;
        }

        void ProductsPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = this.Products;
            this.NameSortButton.DataContext = this.NameSortDirection;
            this.KeyCountSortButton.DataContext = this.KeyCountSortButton;
        }

        private void ProductsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                bool isSelected = false;
                if (this.ProductsListBox.SelectedIndex > -1)
                {
                    isSelected = true;
                }

                this.DeleteButton.Visibility = isSelected ? Visibility.Visible : Visibility.Collapsed;
            }
            catch (Exception ex)
            {
                Logger.WriteLine(LogItemType.Exception, string.Format(CultureInfo.CurrentCulture, "Exception: {0} \r\n {1}", ex.Message, ex.StackTrace));
            }
        }

        private void SettingsButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("../Pages/SettingsPage.xaml", UriKind.Relative));
        }
    }
}
